//
//  WYPersonalController.m
//  CirclePush
//
//  Created by admin on 2018/4/16.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYPersonalController.h"
#import "WYProfileCell.h"
#import "WYHistoryController.h"
#import "WYApplyMemberController.h"
#import "WYMessageController.h"
#import "WYSuggestionController.h"
#import "WYSettingController.h"
#import "WYPusherController.h"
#import "WYUserInfo.h"
//#import "LDCalendarView.h"
@interface WYPersonalController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIView *mainView;

@property (strong, nonatomic) IBOutlet UIView *backgroundView;
@property (strong, nonatomic) IBOutlet UILabel *mobile;

@property (strong, nonatomic) IBOutlet UIImageView *userImageView;

@property (strong, nonatomic) IBOutlet UITableView *settingTableView;

@property (strong, nonatomic) IBOutlet UITableView *systemTableView;
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) IBOutlet UIView *myBackgroundView;
@property (strong, nonatomic) IBOutlet UIView *systemBackgroundView;

@property (nonatomic,strong)NSArray *setImgArray;
@property (nonatomic,strong)NSMutableArray *setNameArray;

@property (nonatomic,strong)NSArray *sysImgArray;
@property (nonatomic,strong)NSArray *sysNameArray;

@property (nonatomic,strong)NSArray *myImgArray;
@property (nonatomic,strong)NSArray *myNameArray;

@property (nonatomic,strong)WYUserInfo *userInfo;

@end

@implementation WYPersonalController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadUserInfo];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupUI];
    [self initData];
}
-(void)loadUserInfo
{
    WEAKSELF
    [WYHttpServer getUserInfoSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            weakSelf.userInfo = [WYUserInfo getUserInfoFromJson:response[@"result"]];
            [weakSelf upData];
            [WYStoreModule stroeUserInfo:weakSelf.userInfo];
        }else{
            [weakSelf toast:errorMessage];
        }
    }];
}
-(void)upData
{
    if ([self.userInfo.twitter isEqualToString:@"2"]) {
        [self.setNameArray replaceObjectAtIndex:2 withObject:@"推客主页"];
    }else if([self.userInfo.twitter isEqualToString:@"1"]) {
        [self.setNameArray replaceObjectAtIndex:2 withObject:@"推客申请"];
    }else{
        [self.setNameArray replaceObjectAtIndex:2 withObject:@"推客申请"];
    }
    self.mobile.text = self.userInfo.telephone;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:self.userInfo.headImage] placeholderImage:[UIImage imageNamed:@"default_header"]];
    
    [self.myTableView reloadData];
    
}

-(void)initData
{
    self.setImgArray = @[@"icon_history",@"icon_collection2",@"icon_twwir"];
    self.setNameArray = @[@"浏览记录",@"我的收藏",@"推客主页"].mutableCopy;
    
    self.sysImgArray = @[@"icon_system",@"icon_system",@"icon_feedback"];
    self.sysNameArray = @[@"联系客服",@"系统消息",@"意见反馈"];
    
    self.myImgArray = @[@"icon_setting"];
    self.myNameArray = @[@"设置"];
}
-(void)setupUI
{
    self.settingTableView.delegate = self;
    self.settingTableView.dataSource = self;
    [self.settingTableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    
    
    
    self.systemTableView.delegate = self;
    self.systemTableView.dataSource = self;
    [self.systemTableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    
    [self.backgroundView setCornerRadius:self.backgroundView.width/2];
    [self.userImageView setCornerRadius:self.userImageView.width/2];

    self.mainView.width = kScreenWidth;
    self.mainView.height = kScreenHeight;
    
    [self.scrollView addSubview:self.mainView];
    [self.scrollView setContentSize:self.mainView.size];
    
    [self.myTableView registerNib:[UINib nibWithNibName:@"WYProfileCell" bundle:nil] forCellReuseIdentifier:@"WYProfileCell"];
    [self.systemTableView registerNib:[UINib nibWithNibName:@"WYProfileCell" bundle:nil] forCellReuseIdentifier:@"WYProfileCell"];
    [self.settingTableView registerNib:[UINib nibWithNibName:@"WYProfileCell" bundle:nil] forCellReuseIdentifier:@"WYProfileCell"];
    //self.scrollView.bounces=NO;
    
    [self.myBackgroundView setBorderWithWidth:1.0 andColor:[UIColor hexStringToColor:@"#EBF3FE" alpha:1.0]];
    [self.myBackgroundView setCornerRadius:5.0f];
    
    [self.systemBackgroundView setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#EBF3FE" alpha:1.0]];
    [self.systemBackgroundView setCornerRadius:5.0f];
    
    [self.settingTableView setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#EBF3FE" alpha:1.0]];
    [self.settingTableView setCornerRadius:5.0f];
    
    self.userImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageAction)];
    [self.userImageView addGestureRecognizer:ges];
    
    
}
-(void)clickImageAction
{
    WEAKSELF
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self; //设置代理
    imagePickerController.allowsEditing = YES;
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [weakSelf presentViewController:imagePickerController animated:YES completion:nil];
        
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [weakSelf presentViewController:imagePickerController animated:YES completion:nil];

        
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [alertController addAction:action3];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //[self showLoading:@""];
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData *data = UIImageJPEGRepresentation(photo, 0.1);
     WEAKSELF
    [WYHttpServer upLoadImage:data SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            [weakSelf editHeadImageWith:response[@"msg"]];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
    }];

}
-(void)editHeadImageWith:(NSString *)headImg
{
    WEAKSELF
    [WYHttpServer editNumber:nil Withpassword:nil WithCode:nil WithHeadImg:headImg SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            [weakSelf loadUserInfo];
        }else{
            
        }
        
        
    }];
    
    
}







-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.systemTableView||tableView==self.myTableView) {
        return 3;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYProfileCell" forIndexPath:indexPath];
    if (tableView == self.myTableView) {
        cell.leftImageView.image = [UIImage imageNamed:self.setImgArray[indexPath.row]];
        cell.name.text = self.setNameArray[indexPath.row];
    }
    if (tableView == self.systemTableView) {
        cell.leftImageView.image = [UIImage imageNamed:self.sysImgArray[indexPath.row]];
        cell.name.text = self.sysNameArray[indexPath.row];
    }
    if (tableView==self.settingTableView) {
        cell.leftImageView.image = [UIImage imageNamed:self.myImgArray[indexPath.row]];
        cell.name.text = self.myNameArray[indexPath.row];
    }
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.myTableView) {
        if (indexPath.row==0) {
            WYHistoryController *hvc = [[WYHistoryController alloc]init];
            hvc.titleName = @"浏览记录";
            hvc.history = @"2";
            [self.navigationController pushViewController:hvc animated:YES];
        }
        if (indexPath.row==1) {
            WYHistoryController *hvc = [[WYHistoryController alloc]init];
           hvc.titleName = @"我的收藏";
            hvc.history = @"1";
            [self.navigationController pushViewController:hvc animated:YES];
        }
        if (indexPath.row==2) {
//            WYPusherController *pvc = [[WYPusherController alloc]init];
//            [self.navigationController pushViewController:pvc animated:YES];
        if ([self.userInfo.twitter isEqualToString:@"2"]) {
            WYPusherController *pvc = [[WYPusherController alloc]init];
            [self.navigationController pushViewController:pvc animated:YES];
        }else if([self.userInfo.twitter isEqualToString:@"1"]) {
            [self toast:@"审核中..."];
        }else{
            WYApplyMemberController *AMVC = [[WYApplyMemberController alloc]init];
            [self.navigationController pushViewController:AMVC animated:YES];
        }
        }
        
    }
    if (tableView==self.systemTableView) {
        if (indexPath.row==0) {
            NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", @"400-6762-9898"];
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
            } else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
            }
        }
        if (indexPath.row==1) {
            WYMessageController *mvc = [[WYMessageController alloc]init];
            [self.navigationController pushViewController:mvc animated:YES];
        }
        
        if (indexPath.row==2) {
            WYSuggestionController *svc = [[WYSuggestionController alloc]init];
            [self.navigationController pushViewController:svc animated:YES];
        }
        
        
        
    }
    if (tableView ==self.settingTableView) {
        WYSettingController *sec = [[WYSettingController alloc]init];
        [self.navigationController pushViewController:sec animated:YES];
    }
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)editAction:(id)sender {
    [self clickImageAction];
}


@end
